// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// s [author] rhyme_lph
// G [email]  rhymelph@gmail.com
// d [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/widgets/button/normal_border_button.dart';
import 'package:finance_app/src/widgets/button/normal_popup_button.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common/pdf_watch_view.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/value_select_form.dart';
import 'package:finance_app/src/widgets/input/form_input_field.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/dashboard/delivery/delivery_order_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';

@RouterMeta(paramName: "deliveryOrderPage")
class DeliveryOrderPage extends StatefulWidget {
  final int id;

  const DeliveryOrderPage({Key key, this.id}) : super(key: key);

  @override
  _DeliveryOrderPageState createState() => _DeliveryOrderPageState();
}

class _DeliveryOrderPageState extends State<DeliveryOrderPage>
    with PagePresenter<DeliveryOrderPage, DeliveryOrderProvider> {
  @override
  void initState() {
    provider.id = widget.id;
    provider.country = Config.defaultCountry;
    super.initState();
  }

  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: 'Delivery Order', actions: [
        listBuilder<bool>(
          selector: (_, __) => __.isShowNext,
          builder: (BuildContext context, bool value, Widget child) =>
              value == true
                  ? NormalNextTextButton(
                      onTap: provider.onNext,
                    )
                  : NormalMorePopupButton(
                      onSelect: provider.onSelectMenu,
                      menuList: provider.menuList,
                    ),
        ),
      ]);

  @override
  Widget get buildWidget => listBuilder<bool>(
        selector: (_, __) => __.isShowNext,
        builder: (BuildContext context, bool value, Widget child) =>
            value == true ? _buildNextLayout() : _buildPdfLayout(),
      );

  Widget _buildNextLayout() => listBuilder<bool>(
        selector: (_, __) => __.isShowNext,
        builder: (BuildContext context, bool value, Widget child) => ListView(
          children: [
            listBuilder<String>(
              selector: (_, __) => __.country?.name,
              builder: (BuildContext context, String value, Widget child) =>
                  ValueSelectForm(
                onTap: provider.onSelectCountry,
                title: S.current.country,
                value: value,
              ),
            ),
            listBuilder<String>(
              selector: (_, __) => __.address,
              builder: (BuildContext context, String value, Widget child) =>
                  ValueSelectForm(
                onTap: provider.onSelectAddress,
                title: S.current.address,
                value: value,
              ),
            ),
            FromInputField(
              controller: provider.unitNoController,
              focusNode: provider.unitNoFocusNode,
              tip: S.current.unit_no,
            ),
            FromInputField(
              controller: provider.postalCodeController,
              focusNode: provider.postalCodeFocusNode,
              tip: S.current.postal_code,
            ),
          ],
        ),
      );

  Widget _buildPdfLayout() => ListView(
        children: [
          AspectRatio(
            aspectRatio: 210 / 297,
            child: PdfWatchView(
              key: provider.pdfKey,
              type: 'quotation',
              id: widget.id,
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(
                horizontal: getDp(38), vertical: getDp(30)),
            child: NormalBorderButton(
              isFit: true,
              title: S.current.share_delivery_order,
              onTap: provider.onShareDeliverOrder,
            ),
          )
        ],
      );
}
